# _*_ coding: UTF-8 _*_
# @Time     : 2021/2/24 上午 10:05
# @Author   : Li Jie
# @Site     : http://www.hzdledu.com/
# @File     : queue_test.py
# @Software : PyCharm

from multiprocessing import Process, Queue
import time


# 进程间通信-Queue队列

# 向队列写入数据
def write(q):
    if not q.full():
        for i in range(5):
            item = '消息' + str(i)
            q.put(item)
            print(f'写入:{item}')


# 从队列读取数据
def read(q):
    time.sleep(1)  # 休眠1秒
    while not q.empty():
        print(f'读取:{q.get(True, 2)}')  # 等待两秒，如果没读到抛出异常


def process_main():
    print('-----------父进程开始----------')
    q = Queue()
    pw = Process(target=write, args=(q,))
    pr = Process(target=read, args=(q,))
    pw.start()
    pr.start()
    pw.join()
    pr.join()
    print('-----------父进程结束----------')


if __name__ == '__main__':
    process_main()
